package com.gis.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.gis.util.JDBCUtils;
import com.gis.util.ResponseUtil;
import com.gis.util.RestResponse;

@WebServlet("/getArea")
public class GetAreaServlet extends HttpServlet {
	QueryRunner qr = new QueryRunner(JDBCUtils.getC3p0DateSource());

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String areaCode = req.getParameter("areaCode") ;
		String sql = "select id, area_code as areaCode, name, lng, lat from cnarea where parent_code = ?";
		
		List<Map<String, Object>> mapList = null;
		try {
			mapList = qr.query(sql, new MapListHandler(), areaCode);
			ResponseUtil.writeJson(resp, RestResponse.success(mapList));
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
